//
//  MySelfInfoVC.m
//  TuYouAPP
//
//  Created by L on 14-2-25.
//
//

#import "MySelfInfoVC.h"
#import "MyPublishVC.h"
#import "MyReplyVC.h"
#import "ModifyUserInfoVC.h"
#import "AboutTuYouVC.h"
#import <AVOSCloud/AVOSCloud.h>
#import <AVOSCloudSNS/AVOSCloudSNS.h>
#import <AVOSCloudSNS/AVUser+SNS.h>
#import "LoginVC.h"
#import "UIImageView+WebCache.h"
#import "AppDelegate.h"
#import "ReplayMeVC.h"
@interface MySelfInfoVC ()

@end

@implementation MySelfInfoVC
{
    BOOL _bhaveweibo;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}
-(void)upDateView{
    
    NSString *namestr =[TYUser currentUser].mUserDisplayName;

    NSURL *url = [NSURL URLWithString:[TYUser currentUser].mHeadImgURL];
    [self.headImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"70_70.png"]];
    CGSize constraint = CGSizeMake(290, CGFLOAT_MAX);
    CGSize size ;
    CGRect rect;
        self.userName.text =namestr ;
    size = [namestr sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
    
    self.userAddr.text = [[AVUser currentUser]objectForKey:@"userPointInfo"];
    rect = self.userSex.frame;
    rect.origin.x = size.width+84;
    self.userSex.frame = rect;
    
    
    constraint = CGSizeMake(280, CGFLOAT_MAX);
    if ([TYUser currentUser].mUserSign.length == 0) {
        self.signContentView.hidden = YES;
    }
    else
    {
        self.signContentView.hidden = NO;
    }
    size = [[TYUser currentUser].mUserSign sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
    rect = self.userSign.frame;
    rect.origin.y = 5;
    rect.size.height = size.height+5;
    self.userSign.frame = rect;
    rect = self.signContentView.frame;
    rect.size.height =self.userSign.frame.size.height+10;
    self.signContentView.frame = rect;
    self.duijiang.text = [TYUser currentUser].mDuijiang;
   // self.userSign.center = self.signContentView.center;
    rect = self.midView.frame;
    rect.origin.y= self.signContentView.frame.size.height+self.signContentView.frame.origin.y+15;

    self.midView.frame = rect;
    rect = self.botView.frame;
    rect.origin.y= self.midView.frame.size.height+self.midView.frame.origin.y+15;
    self.botView.frame = rect;
    
    rect = self.logoutbt.frame;
    rect.origin.y= self.botView.frame.size.height+self.botView.frame.origin.y+15;
    self.logoutbt.frame = rect;
    
    if (!DeviceIsiPhone5()) {
        self.contentSrcollview.contentSize = CGSizeMake(320, self.logoutbt.frame.size.height+self.logoutbt.frame.origin.y+10);
    }
    else
    {
        if (DeviceIsiPhone5()) {
            self.contentSrcollview.contentSize = CGSizeMake(320, self.logoutbt.frame.size.height+self.logoutbt.frame.origin.y+20);
        }

    }


    self.userSign.text = [TYUser currentUser].mUserSign;
    self.userSex.image = [TYUser currentUser].mSex==1?[UIImage imageNamed:@"women.png"]:[UIImage imageNamed:@"men.png"];

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self upDateView];
}
- (void)viewDidLoad
{
    self.mPageName = @"我的";
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.Title = @"我的";
    self.LeftBarTitle = @"返回";
    self.signContentView.layer.borderWidth = 0.5;
    self.signContentView.layer.borderColor = [ColorRGB(212, 212, 212)CGColor];
    self.midView.layer.borderWidth = 0.5;
    self.midView.layer.borderColor = [ColorRGB(212, 212, 212)CGColor];
    self.botView.layer.borderWidth = 0.5;
    self.botView.layer.borderColor = [ColorRGB(212, 212, 212)CGColor];
    self.RightBarTitle = @"设置";


    
    if (!DeviceIsiPhone5()) {
        self.contentSrcollview.contentSize = CGSizeMake(320, 520);
    }
    
    [[TYUser currentUser]getMyPublishCount:^(int c) {
        NSString *str = [NSString stringWithFormat:@"%d",c];
        [self.publushCount setText:str];
    }];
    
    [[TYUser currentUser]getMyCommentCount:^(int c) {
        NSString *str = [NSString stringWithFormat:@"%d",c];
        [self.replyCount setText:str];
    }];
    [[TYUser currentUser]getReplyMeCount:^(int count, NSString *error) {
        if (!error) {
            NSString *str = [NSString stringWithFormat:@"%d",count];
            [self.replyMe setText:str];
        }
        
    }];
    

    
    AVUser* user = [AVUser currentUser];
    if( user )
    {//
        if( [[user objectForKey:@"authData"] objectForKey:@"weibo"] )
        {//有微博绑定,,
            [self updateSomeWeiboUI:YES];
        }
        else
        {
            [self updateSomeWeiboUI:NO];
        }
    }
    
    TYUser* ityuser = [TYUser currentUser];
    if( ityuser )
    {
        [ityuser getMyFlag:^(NSInteger flag, NSString *eror) {
            
            _myreply.hidden = !( flag & E_flag_have_reback );
            
            _commentMe.hidden = !( flag & E_flag_have_reply );
            
        }];
    }
    
}
-(void)updateSomeWeiboUI:(BOOL)bhaveweibol
{
    if( bhaveweibol )
    {//有微博绑定,,
        _logoutbt.hidden = NO;
        [_sinabt setTitle:@"已登录新浪微博" forState:UIControlStateNormal];
       
        CGSize constraint = CGSizeMake(290, CGFLOAT_MAX);
        CGSize size ;
        CGRect rect;
        size = [ [TYUser currentUser].mUserDisplayName sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
        
        rect = self.sinaLogo.frame;
        rect.origin.x = 268-size.width-40;
        self.sinaLogo.frame = rect;
        self.sinaNameLabel.text = [TYUser currentUser].mUserDisplayName;
        _bhaveweibo =YES;
    }
    else
    {
        _logoutbt.hidden = YES;
        CGRect rect =  self.sinaLogo.frame;
        rect.origin.x = 225;
        self.sinaLogo.frame = rect;
        
        [_sinabt setTitle:@"新浪微博登录" forState:UIControlStateNormal];
        _bhaveweibo = NO;
    }
}

- (IBAction)replyMeBtnTouched:(id)sender {
    ReplayMeVC *vc = [[ReplayMeVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    _commentMe.hidden = YES;
    
}
-(void)rightBtnTouched:(id)sender
{
    ModifyUserInfoVC *vc = [[ModifyUserInfoVC alloc]initWithNibName:@"ModifyUserInfoVC" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)logoutBtnTouched:(id)sender {
     
    [AVUser logOut];
    [AVOSCloudSNS logout:AVOSCloudSNSSinaWeibo];
    //跳转
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (IBAction)aboutBtnTouched:(id)sender {
    AboutTuYouVC *vc = [[AboutTuYouVC alloc]initWithNibName:@"AboutTuYouVC" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)feedbackBtnTouched:(id)sender {
    
}
- (IBAction)sinaBtnTouched:(id)sender {
    
    if( _bhaveweibo )
    {
        return;
    }
    [GInfo shareClient].isAVOSCloud = NO;
    [AVOSCloudSNS setupPlatform:AVOSCloudSNSSinaWeibo withAppKey:SINA_KEY andAppSecret:SINA_SEC andRedirectURI:REDI_URI];
    
    [AVOSCloudSNS loginWithCallback:^(id object, NSError *error) {
        
        AVUser* nowuser = [AVUser currentUser];
        
        [nowuser addAuthData:object block:^(AVUser *user, NSError *error) {
            
            if ( user )
            {
                [LoginVC updateUserWeiBoInfo:object user:user];
                [self updateSomeWeiboUI:YES];
            }
            else
            {
                [SVProgressHUD showErrorWithStatus:@"绑定失败."];
                [self updateSomeWeiboUI:YES];
            }
        }];
        
    } toPlatform:AVOSCloudSNSSinaWeibo];
}
- (IBAction)huifuBtnTouched:(id)sender {
    MyReplyVC *vc = [[MyReplyVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    _myreply.hidden = YES;
}
- (IBAction)fabuBtnTouched:(id)sender {
    MyPublishVC *vc = [[MyPublishVC alloc]init];
    vc.user = [TYUser currentUser];
    vc.fromMyself = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)duijiangjiBtnTouched:(id)sender {
}

-(void)leftBtnTouched:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
